<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import = "com.classes.Casemast, java.util.ArrayList, java.util.TreeMap, org.owasp.encoder.Encode, java.text.DecimalFormat" %>
<%@ page import = "com.mbbmap.security.dao.*" %>
<%@ include file="/jsp/map/include/user-dao.jsp" %>	
<%@page import="com.mbbmap.util.EncryptionHelper"%>
<%

TreeMap<String, String> search = (TreeMap<String, String>) request.getAttribute("search");

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="/jsp/map/include/html-meta.jsp" />
<title>MAYBANK</title>
<script src="<%= request.getContextPath() %>/js/report.js"></script>
</head>

<body onload="report.region_sla_init();">

<!-- Header -->
<%@ include file="/jsp/common/Header.jsp" %>	
<!-- End Header -->


<div class="row-fluid page" style="width:1200px; max-width:none;">
	<div class="span12">
		
		<jsp:include page="/jsp/map/include/report-nav.jsp">
			<jsp:param name="report_title" value='<%=EncryptionHelper.encrypt("Region Report (Non MY)") %>' />
		</jsp:include>

		<form name="search_form" method="post" action="<%= request.getContextPath() + "/Report.do?dispatch=region_sla" %>" onsubmit="return report.search_region_sla_validate();">
			<div class="row-fluid form-horizontal search">
				<div class="span6">
				
					<div class="control-group">
						<label class="control-label">Application Type</label>
						<div class="controls">
							<select id="application_type" name="application_type">
							<% 
								String html = "<option value=\"\"></option>"
									+ "<option value=\"New Application\">New Application</option>"
									+ "<option value=\"Extend Facility\">Extend Facility</option>"
									+ "<option value=\"e-Commerce/M2u\">e-Commerce/M2u</option>"
									+ "<option value=\"Additional Terminal/mPOS Reader\">Additional Terminal/mPOS Reader</option>";
								
								if(search.containsKey("application_type") && search.get("application_type").length() > 0){
									html = html.replace("value=\"" + search.get("application_type") + "\"", "value=\"" + search.get("application_type") + "\" selected ");
								}
								
								out.println(html);
							%>
							</select>
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Period</label>
						<div class="controls">
							<input type="text" id="from_date" name="from_date" placeholder="eg. 23/01/2015" value="<%=search.containsKey("from_date") ? search.get("from_date") : "" %>" style="width:88px" /> to
							<input type="text" id="to_date" name="to_date" placeholder="eg. 09/02/2015" value="<%=search.containsKey("to_date") ? search.get("to_date") : "" %>" style="width:88px" />
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Month</label>
						<div class="controls">
							<select id="month" name="month" style="width:115px">
							<% 
								html = "<option value=\"\"></option>"
									+ "<option value=\"01\">January</option>"
									+ "<option value=\"02\">February</option>"
									+ "<option value=\"03\">March</option>"
									+ "<option value=\"04\">April</option>"
									+ "<option value=\"05\">May</option>"
									+ "<option value=\"06\">June</option>"
									+ "<option value=\"07\">July</option>"
									+ "<option value=\"08\">August</option>"
									+ "<option value=\"09\">September</option>"
									+ "<option value=\"10\">October</option>"
									+ "<option value=\"11\">November</option>"
									+ "<option value=\"12\">December</option>";
								
								if(search.containsKey("month") && search.get("month").length() > 0){
									html = html.replace("value=\"" + search.get("month") + "\"", "value=\"" + search.get("month") + "\" selected ");
								}
								
								out.println(html);
							%>
							</select>
							
							<input type="text" id="year" name="year" placeholder="eg. 2015" value="<%=search.containsKey("year") ? search.get("year") : "" %>" style="width:88px" />
						</div>
					</div>
					
				</div>
				
				<div class="span6">
				
					<div class="control-group">
						<label class="control-label">Group</label>
						<div class="controls">
							<select id="group_code" name="group_code">
							<% 
								html = "<option value=\"\"></option>";
								for(SecGroupsDao ug : (ArrayList<SecGroupsDao>) request.getAttribute("groupList")){
									html += "<option value=\"" + ug.getGroupCode() + "\">" + ug.getGroupName() + "</option>";
								}
								
								if(search.containsKey("group_code") && search.get("group_code").length() > 0){
									html = html.replace("value=\"" + search.get("group_code") + "\"", "value=\"" + search.get("group_code") + "\" selected ");
								}
								
								out.println(html);
							%>
							</select>
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">User</label>
						<div class="controls">
							<select id="group_user" name="group_user">
							<%
								html = "<option value=\"\"></option>";
								for(SecUserDao u : (ArrayList <SecUserDao>) request.getAttribute("userList")){
									html += "<option value=\"" + u.getLogonId() + "\">" + u.getUserName() + "</option>";
								}
								
								if(search.containsKey("group_user") && search.get("group_user").length() > 0){
									html = html.replace("value=\"" + search.get("group_user") + "\"", "value=\"" + search.get("group_user") + "\" selected ");
								}
								
								out.println(html);
							%>
							</select>
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Region</label>
						<div class="controls">
							<select id="reseller_location" name="reseller_location">
							<% 
								html = "<option value=\"\"></option>"
									+ "<option value=\"SABAH\">SABAH</option>"
									+ "<option value=\"SARAWAK\">SARAWAK</option>"
									+ "<option value=\"PERLIS\">PERLIS</option>"
									+ "<option value=\"KEDAH\">KEDAH</option>"
									+ "<option value=\"PULAU PINANG\">PULAU PINANG</option>"
									+ "<option value=\"PERAK\">PERAK</option>"
									+ "<option value=\"KELANTAN\">KELANTAN</option>"
									+ "<option value=\"TERENGGANU\">TERENGGANU</option>"
									+ "<option value=\"PAHANG\">PAHANG</option>"
									+ "<option value=\"SELANGOR\">SELANGOR</option>"
									+ "<option value=\"NEGERI SEMBILAN\">NEGERI SEMBILAN</option>"
									+ "<option value=\"MELAKA\">MELAKA</option>"
									+ "<option value=\"JOHOR\">JOHOR</option>"
									+ "<option value=\"WILAYAH PERSEKUTUAN\">WILAYAH PERSEKUTUAN</option>";
								
								if(search.containsKey("reseller_location") && search.get("reseller_location").length() > 0){
									html = html.replace("value=\"" + search.get("reseller_location") + "\"", "value=\"" + search.get("reseller_location") + "\" selected ");
								}
								
								out.println(html);
							%>
							</select>
						</div>
					</div>
					
				</div>
			</div>
			
			<div class="row-fluid">
				<div class="span12 btn-control search">
					<button id="search_submit" name="search_submit" type="submit" class="btn">Search</button>
					<button id="search_clear" name="search_clear" type="submit" class="btn">Clear</button>
					<button id="export_submit" name="export_submit" type="submit" class="btn">Export Data</button>
				</div>
			</div>
			
		</form>
		
		<div style="width:1200px; overflow-x:auto">
		<table class="tblDetails reportTbl"><thead>
			<tr>
				<th rowspan="2">No.</th>
				<th rowspan="2">MAP Reference No.</th>
				<th rowspan="2">Application Type</th>
				<th rowspan="2">Group Name</th>
				<th rowspan="2">Creator</th>
				<th rowspan="2">Process 2-5 Status</th>
				<th rowspan="2">Process 2-5 SLA</th>
				<th rowspan="2">Process 6 Status</th>
				<th rowspan="2">Process 6 SLA</th>
				<th rowspan="2">Process 7 Status</th>
				<th rowspan="2">Process 7 SLA</th>
				<th class="num" colspan="4">TAT</th>
			</tr>
			<tr>
				<th>Current</th>
				<th>End to end</th>
			</tr>
		</thead><tbody>
		<%
			int n = 0;
			int process_1_5 = 0; double process_1_5C = 0.0;
			int process_6 = 0; double process_6C = 0.0;
			int process_7 = 0; double process_7C = 0.0;
			int current_sla = 0; double current_slaC = 0.0;
			int end_to_end_sla = 0; double end_to_end_slaC = 0.0;
			
			for(TreeMap<String, String> r : (ArrayList<TreeMap<String, String>>) request.getAttribute("rArr")){
				if(r.get("process_1_5") != null) {
					process_1_5C++;
					process_1_5 += Integer.parseInt(r.get("process_1_5"));
				}
				
				if(r.get("process_6") != null) {
					process_6C++;
					process_6 += Integer.parseInt(r.get("process_6"));
				}
				
				if(r.get("process_7") != null) {
					process_7C++;
					process_7 += Integer.parseInt(r.get("process_7"));
				}
				
				if(r.get("current_sla") != null) {
					current_slaC++;
					current_sla += Integer.parseInt(r.get("current_sla"));
				}
				
				if(r.get("end_to_end_sla") != null) {
					end_to_end_slaC++;
					end_to_end_sla += Integer.parseInt(r.get("end_to_end_sla"));
				}
				
				String status = null;
				if(r.get("status_5a") != null) {
					status = r.get("status_5a");
				} else if(r.get("status_5") != null) {
					status = r.get("status_5");
				} else if(r.get("status_4a") != null) {
					status = r.get("status_4a");
				} else if(r.get("status_4") != null) {
					status = r.get("status_4");
				} else if(r.get("status_3") != null) {
					status = r.get("status_3");
				} else if(r.get("status_2") != null) {
					status = r.get("status_2");
				} else if(r.get("status_1") != null) {
					status = r.get("status_1");
				}
				
				html = "<tr class=" + (n % 2 > 0 ? "alt" : "even") + ">"
					+ "		<td>" + (n+1) + "</td>"
					+ "		<td>" + (r.get("reference_no") == null ? "-" : Encode.forHtml(r.get("reference_no"))) + "</td>"
					+ "		<td>" + (r.get("type") == null ? "-" : Encode.forHtml(r.get("type"))) + "</td>"
					+ "		<td>" + (r.get("group_name") == null ? "-" : Encode.forHtml(r.get("group_name"))) + "</td>"
					+ "		<td>" + (r.get("creator") == null ? "-" : Encode.forHtml(r.get("creator"))) + "</td>"
					+ "		<td>" + (status == null ? "-" : Encode.forHtml(status)) + "</td>"
					+ "		<td class='num'>" + (r.get("process_1_5") == null ? "-" : Encode.forHtml(r.get("process_1_5"))) + "</td>"
					+ "		<td>" + (r.get("status_6") == null ? "-" : Encode.forHtml(r.get("status_6"))) + "</td>"
					+ "		<td class='num'>" + (r.get("process_6") == null ? "-" : Encode.forHtml(r.get("process_6"))) + "</td>"
					+ "		<td>" + (r.get("status_7") == null ? "-" : Encode.forHtml(r.get("status_7"))) + "</td>"
					+ "		<td class='num'>" + (r.get("process_7") == null ? "-" : Encode.forHtml(r.get("process_7"))) + "</td>"
					+ "		<td class='num'>" + (r.get("current_sla") == null ? "-" : Encode.forHtml(r.get("current_sla"))) + "</td>"
					+ "		<td class='num'>" + (r.get("end_to_end_sla") == null ? "-" : Encode.forHtml(r.get("end_to_end_sla"))) + "</td>"
					+ "</tr>";
				
				out.println(html);
				++n;
			}
			
			html = "<tr style='background:#333; color:#fff'>"
				+ "		<td></td>"
				+ "		<td></td>"
				+ "		<td></td>"
				+ "		<td></td>"
				+ "		<td></td>"
				+ "		<td></td>"
				+ "		<td class='num'>" + (process_1_5C > 0 ? String.format("%.2f", process_1_5/process_1_5C) : "-") + "</td>"
				+ "		<td></td>"
				+ "		<td class='num'>" + (process_6C > 0 ? String.format("%.2f", process_6/process_6C) : "-") + "</td>"
				+ "		<td></td>"
				+ "		<td class='num'>" + (process_7C > 0 ? String.format("%.2f", process_7/process_7C) : "-") + "</td>"
				+ "		<td class='num'>" + (current_slaC > 0 ? String.format("%.2f", current_sla/current_slaC) : "-") + "</td>"
				+ "		<td class='num'>" + (end_to_end_slaC > 0 ? String.format("%.2f", end_to_end_sla/end_to_end_slaC) : "-") + "</td>"
				+ "</tr>";
			
			out.println(html);
		%>
		</tbody></table>
		</div>
		
		
		
		<table class="tblDetails reportTbl" style="width:30%"><thead>
			<tr><th colspan="3">Total Applications by Regional</th></tr>
		</thead><tbody>
			<%
				int total_count = 0;
			
				for(TreeMap<String, String> r : (ArrayList<TreeMap<String, String>>) request.getAttribute("summary")){
					total_count += Integer.parseInt(r.get("count"));
					
					html = "<tr>"
						+ "		<td>" + (r.get("country_code") == null ? "-" : Encode.forHtml(r.get("country_code"))) + "</td>"
						+ "		<td><b>" + (r.get("count") == null ? "-" : Encode.forHtml(r.get("count"))) + "</b></td>"
						+ "</tr>";
						
					out.println(html);
				}
			%>
			<tr>
				<td><b>Grand Total</b></td>
				<td style="border-top:1px solid #000; border-bottom-style:double"><b><%= total_count %></b></td>
			</tr>
		</tbody></table>
		
	</div>
</div>

<!-- Footer -->
<%@ include file="/jsp/common/Footer.jsp" %>	
<!-- End Footer -->


</body>
</html>